package com.lovo.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.stock.bean.JournalBean;
import com.lovo.stock.bean.PageInfo;
import com.lovo.stock.bean.UserBean;
import com.lovo.stock.mapper.JournalMapper;
import com.lovo.stock.service.IJournalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;

@Service
public class JournalServiceImpl extends ServiceImpl<JournalMapper, JournalBean> implements IJournalService {

    @Autowired
    JournalMapper journalMapper;

    @Override
    public IPage<JournalBean> queryJournalByPage(PageInfo<JournalBean> pageInfo) {
        QueryWrapper q=new QueryWrapper();
        if (pageInfo.getStartTime()!=null&&!"".equals(pageInfo.getStartTime())){
            q.ge("journal_date",pageInfo.getStartTime());
        }
        if (pageInfo.getEndTime()!=null&&!"".equals(pageInfo.getEndTime())){
            q.le("journal_date",pageInfo.getEndTime());
        }
        IPage<JournalBean> page=journalMapper.selectPage(new Page<>(pageInfo.getCurPage(), pageInfo.getPageSize()),q);
        return page;
    }

    @Override
    public void addJournal(String str,UserBean userBean) {
        JournalBean journalBean=new JournalBean();
        //日志时间
        journalBean.setJournalDate(LocalDate.now().toString());
        //日志内容
        String time= LocalTime.now().toString();
        String newTime=time.substring(0,5);
        journalBean.setJournalContent(userBean.getUserRealName()+"在"+newTime+str);
        journalMapper.insert(journalBean);
    }
}
